var settings = settings ||{};
var pageContext = new PageContext(settings||{});

pageContext.back = function(target){
	var $lis = $("#page-header>ul>li");
	var len = $lis.length ;
	$($lis.get(len-2)).find("a").click();
}

pageContext.addItemAfter = function(result,para){
	pageContext.back();
};
pageContext.updateItemAfter = function(result,para){
	pageContext.back();
};
//***************input tree**********************//
var pageContextTree = new PageContextTree(settings||{});
pageContextTree.hasInstance = false;
pageContextTree.reload = function(){
	var nodes = pageContextTree.getNodesFromParent("type","instance",null)
	if(nodes && nodes.length>0){
		pageContextTree.hasInstance = true;
		for(var i in nodes){
			var obj = nodes[i];
			pageContextTree.treeObj.reAsyncChildNodes(obj, "refresh");
		}
	}else{
		pageContextTree.treeObj.reAsyncChildNodes(null, "refresh");
	}
}
pageContext.tree = function(id,instance,option){
	pageContextTree.userId = id;
	if(instance && instance!="undefined"){
		pageContextTree.openDialog(id,null,option)
	}else{
		$.ajax({
			url:window.global.getFinalUrl(window.global.ctx+"/sys/instance/list")
			,async:true
			,type: "get"
				,dataType:"json"
					,success:function(result){
						if(result.status==200){
							var list = result.data.list;
							var array = null;
							if(list && list.length>1){
								array = [];
								for(var i in list){
									var obj = list[i];
									var item = {};
									item.name = obj.nameStr;
									item.id = null;
									item.type = "instance";
									item.instanceId = obj.id;
									item.title = obj.remark;
									item.nocheck = true;
									item.isParent = true;
									array.push(item);
								}
							}else if(list && list.length==1){
								var obj = list[0];
								setting.async.url= window.global.addPara(setting.async.url,"instanceId",obj.id,true);
							}
							pageContextTree.openDialog(id,array,option)
							ok = true;
						}else{
							ok = false;
						}
					}
		,error: function(XMLHttpRequest, textStatus, errorThrown) {
			ok = false;
		}
		});
	}
}
pageContextTree.openDialog = function(id,data,option){
	var layero = $("#applyeditForm");
	var tree = $(layero.find(".ztree")[0]);
	tree.attr("id","FormDialog"+"ztree"+id);
	pageContextTree.treeObj = $.fn.zTree.init(tree, setting, data);
	if(data != null){
		pageContextTree.reload();
	}
}
pageContextTree.check = function(first){
	var nodes = pageContextTree.treeObj.getCheckedNodes(true);
	var array = [];
	if(nodes && nodes.length>0){
		for(n in nodes){
			array.push(nodes[n].id);
		}
	}
	if(first){
		$("#applyeditForm input[name='ids']").val(array.join(",")).attr("oldval",array.join(","));
	}else{
		$("#applyeditForm input[name='ids']").val(array.join(","))
	}
}
pageContextTree.userId = null;
var setting = {
	async: {
		enable: true,
		type: "get",
		url: window.global.getFinalUrl(window.global.ctx+"/auth/userrole/tree"),
		otherParam:{'hasInstance':window.global.hasInstance,'userId':function(){ return pageContextTree.userId }},
		autoParam: ["instanceId"]
	},
	check: {
		enable: true
		,chkboxType :{ "Y" : "", "N" : "" }
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		onCheck: zTreeOnCheck
		,onAsyncSuccess: zTreeOnAsyncSuccess
		,onClick: zTreeOnClick
	}
};
function zTreeOnClick(event, treeId, treeNode) {
	pageContextTree.treeObj.checkNode(treeNode,null,true,true);
	return true;
};
function zTreeOnCheck(event, treeId, treeNode) {
    pageContextTree.check();
};
function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
	pageContextTree.check(true);
};
jQuery(function($) {
	 $(document).find("input").each(function(){
		 $(this).attr("autocomplete","off");
	 });
	pageContext.init();
	pageContext.tree($("#applyeditForm input[name='id']").val()
			,$("#applyeditForm input[name='instanceId']").val());
}); 
